from flask import render_template, jsonify,redirect,url_for
from jieba.analyse import extract_tags
import spider

from app import app, utils


def searchlist(list, v, res):
    if (list.count(v) == 0):
        return
    num = list.index(v)
    res.append(num)
    list.pop(num)
    searchlist(list, v, res)

@app.route('/hello')
def hello():
    return 'hello'


@app.route('/')
def index():
    return render_template('main.html')


@app.route('/time')
def get_time():
    return utils.get_time()


@app.route('/baidu')
def get_baidu_data():
    data = utils.get_baidu_data()
    d = []
    for i in data:
        ks = extract_tags(i[0])
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": i[1]})
    # print(d)
    # ----------------------
    c = []
    name = []
    value = []
    for dd in d:
        name.append(dd['name'])
        value.append(dd['value'])
    lst = list(set(name))
    valist = [0 for i in range(len(lst))]



    for i in range(len(lst)):
        vaflag = []
        searchlist(name, lst[i], vaflag)
        for j in vaflag:
            valist[i] += value[j]
        c.append({"name": lst[i], "value": valist[i]})
    return jsonify({"kws": c})


@app.route('/weibo')
def get_weibo_data():
    data = utils.get_weibo_data()
    d = []
    for i in data:
        ks = extract_tags(i[0])
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": i[1]})
    # print(d)
    # ----------------------
    c = []
    name = []
    value = []
    for dd in d:
        name.append(dd['name'])
        value.append(dd['value'])
    lst = list(set(name))
    valist = [0 for i in range(len(lst))]

    for i in range(len(lst)):
        vaflag = []
        searchlist(name, lst[i], vaflag)
        for j in vaflag:
            valist[i] += value[j]
        c.append({"name": lst[i], "value": valist[i]})
    return jsonify({"kws": c})


@app.route('/zhihu')
def get_zhihu_data():
    data = utils.get_zhihu_data()
    d = []
    for i in data:
        ks = extract_tags(i[0])
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": i[1]})
    # print(d)
    # ----------------------
    c = []
    name = []
    value = []
    for dd in d:
        name.append(dd['name'])
        value.append(dd['value'])
    lst = list(set(name))
    valist = [0 for i in range(len(lst))]

    for i in range(len(lst)):
        vaflag = []
        searchlist(name, lst[i], vaflag)
        for j in vaflag:
            valist[i] += value[j]
        c.append({"name": lst[i], "value": valist[i]})
    return jsonify({"kws": c})

@app.route('/bzhan')
def get_bzhan_data():
    data = utils.get_bzhan_data()
    d = []
    for i in data:
        ks = extract_tags(i[0])
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": i[1]})
    # print(d)
    # ----------------------
    c = []
    name = []
    value = []
    for dd in d:
        name.append(dd['name'])
        value.append(dd['value'])
    lst = list(set(name))
    valist = [0 for i in range(len(lst))]

    for i in range(len(lst)):
        vaflag = []
        searchlist(name, lst[i], vaflag)
        for j in vaflag:
            valist[i] += value[j]
        c.append({"name": lst[i], "value": valist[i]})
    return jsonify({"kws": c})


@app.route('/pa',methods=["GET","POST"])
def pa():
    spider.pa()
    return redirect(url_for('index'))